// 封装get和post请求

// get
export let getData = (url, params) => {
    // 参数处理
    if (params) {
        let paramsArray = [];
        Object.keys(params).forEach( key => paramsArray.push( key + '=' + params[key] ) )
        // console.log(paramsArray);
        if ( url.search(/\?/) === -1 ) {
            url += '?' + paramsArray.join('&')
        }else {
            url += paramsArray.join('&')
        }
    }

    let Promise = fetch(url, {
        method: 'GET'
    }).then(res => res.json())
    return Promise
}

// post
export let postData = (url, data) => {
    let Promise = fetch(url, {
        body: JSON.stringify(data),
        headers: {
            'content-type': 'application/json'
        },
        method: 'POST'
    }).then(res => res.json())
    return Promise
}